//
//  DDetailViewController.m
//  Dear
//
//  Created by n22 on 15/7/15.
//  Copyright (c) 2015年 陈俊俊. All rights reserved.
//

#import "DDetailViewController.h"
#import "SDetailView.h"
#import "CommentCell.h"
#import "CommentViewController.h"
@interface DDetailViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
{
    SDetailView *_detailView;
    NSMutableArray *_dataArr;
    UITextField *_commentTextFiled;
}
@end

@implementation DDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBA(246, 246, 246, 1);
    _dataArr = [[NSMutableArray alloc]init];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenSize.width, ScreenSize.height - 64 - 50) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    _detailView = [[[NSBundle mainBundle]loadNibNamed:@"SDetailView" owner:self options:nil]lastObject];
    _detailView.model = self.model;
    _detailView.frame = CGRectMake(0, 0, ScreenSize.width, _detailView.totalH);
    [self.view addSubview:_detailView];
    self.tableView.tableHeaderView = _detailView;
    
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(self.tableView.frame)+ 5 , ScreenSize.width- 20, 40)];
    topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topView];
    
    UIImageView *leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 12, 16, 16)];
    leftImageView.image = [UIImage imageNamed:@"commentInput"];
    leftImageView.contentMode = UIViewContentModeScaleAspectFit;
    [topView addSubview:leftImageView];
    //评论
    _commentTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(leftImageView.frame) + 5, 0, CGRectGetWidth(topView.frame) - CGRectGetMaxX(leftImageView.frame), 40)];
    _commentTextFiled.placeholder = @"写点什么吧...";
    _commentTextFiled.font = FontSize(15);
    _commentTextFiled.delegate = self;
    _commentTextFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    [topView addSubview:_commentTextFiled];
    
    //发送请求
    [self getCommentData];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if ([DearUser sharedInstance].isRefresh) {
        [self getCommentData];
    }
}

#pragma mark -获取请轮接口
- (void)getCommentData{
    [DearUser sharedInstance].isRefresh = NO;
    //评论
    NSDictionary *dict = @{@"userId":[DearUser sharedInstance].userId,@"subjectId":self.model.id};
    NSString *url = [ServerUrl stringByAppendingFormat:@"/%@",DearHttpQueryComment];
    [[HttpRequest sharedInstance] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [DearLoadingView hideHUD];
        if (responseObject) {
            NSLog(@"%@",UUIDStr);
            NSDictionary *dict = (NSDictionary *)responseObject;
            if ([dict[@"Result"] integerValue] == 0) {
                if(_dataArr.count > 0){
                    [_dataArr removeAllObjects];
                }
                NSArray *itemArr = dict[@"item"];
                for (NSDictionary *itemDict in itemArr) {
                        CommentModel *commentModel = [[CommentModel alloc]init];
                        [commentModel setValuesForKeysWithDictionary:itemDict];
                        NSDictionary *userDict = itemDict[@"userItem"];
                        commentModel.userCode = userDict[@"userCode"];
                        commentModel.userName = userDict[@"userName"];
                        commentModel.userRoles = userDict[@"userRoles"];
                        commentModel.picture = userDict[@"picture"];
                        [_dataArr addObject:commentModel];
                }
                [_tableView reloadData];
            }else {
                [DearLoadingView showTips:dict[@"Message"]];
            }
        }else{
            [DearLoadingView showTips:@"获取失败"];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [DearLoadingView hideHUD];
        [DearLoadingView showTips:error];
    }];

}

#pragma  mark - tableView协议
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellID = @"CommentCell";
    CommentCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"CommentCell" owner:self options:nil]lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (_dataArr.count > 0) {
        CommentModel *commentModel = _dataArr[indexPath.row];
        cell.commentModel = commentModel;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommentModel *commentModel = _dataArr[indexPath.row];
    CGFloat conH = 0;
    if (commentModel.content.length > 0) {
        conH = [CJJHelper textHeightFromString:commentModel.content width:ScreenSize.width - 20 fontSize:15];
    }
    CGFloat totalHeight = conH + 100;
    if (totalHeight > 110) {
        return totalHeight;
    }
    return 110;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    [textField resignFirstResponder];
    CommentViewController *cvc = [[CommentViewController alloc]init];
    cvc.subjectId = self.model.id;
    [self.navigationController pushViewController:cvc animated:YES];
    
}


@end
